package com.ruoyi.kindergarten.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.kindergarten.domain.EnvironmentInfo;
import com.ruoyi.kindergarten.domain.SimpleDeviceValue;
import com.ruoyi.kindergarten.domain.TwinPhysicalDevice;
import com.ruoyi.kindergarten.mapper.EnvironmentInfoMapper;
import com.ruoyi.kindergarten.mapper.TwinPhysicalDeviceMapper;
import com.ruoyi.kindergarten.service.ITwinPhysicalDeviceService;
import com.ruoyi.kindergarten.util.HttpClientUtil;
import com.ruoyi.kindergarten.websocket.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class TwinPhysicalDeviceServiceImpl implements ITwinPhysicalDeviceService {
    @Autowired
    private TwinPhysicalDeviceMapper twinPhysicalDeviceMapper;

    @Autowired
    private EnvironmentInfoMapper environmentInfoMapper;

    @Autowired
    private WebSocketService webSocketService;

    private static final String url = "http://10.196.101.27:1662/thing/provider/rest/monitor/dynamics/2306328859501170";


    @Override
    public void post(TwinPhysicalDevice twinPhysicalDevice) {
        TwinPhysicalDevice tpd =twinPhysicalDeviceMapper.selectTwinPhysicalDevice(twinPhysicalDevice);;
//
        if (tpd == null) {
            tpd = new TwinPhysicalDevice();
            tpd.setPhysical("0503000C0001458D");
            tpd.setTwin("噪声传感器101");
            tpd.setId(6L);
            tpd.setMetric("噪声");
        }
        EnvironmentInfo environmentInfo = new EnvironmentInfo();
        environmentInfo.setDeviceName(tpd.getTwin());
        List<EnvironmentInfo> environmentInfos = environmentInfoMapper.selectEnvironmentInfoList(environmentInfo);
        EnvironmentInfo environmentInfo1 = environmentInfos.get(0);
        Long deviceStatus = environmentInfo1.getDeviceStatus();
        if(deviceStatus == 0L){
            environmentInfo1.setDeviceStatus(1L);
            environmentInfoMapper.updateEnvironmentInfo(environmentInfo1);
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                String s = objectMapper.writeValueAsString(environmentInfo1);
                webSocketService.onMessage(s);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        SimpleDeviceValue simpleDeviceValue = new SimpleDeviceValue();
        simpleDeviceValue.setDeviceName(tpd.getTwin());
        simpleDeviceValue.setValue(twinPhysicalDevice.getValue());
        simpleDeviceValue.setTime(new Date());
        twinPhysicalDeviceMapper.addSimpleDeviceValue(simpleDeviceValue);
    }

    @Override
    public Double getValue(String key) {
        SimpleDeviceValue simpleDeviceValue = new SimpleDeviceValue();
        simpleDeviceValue.setDeviceName(key);
        SimpleDeviceValue simpleDeviceValue2 =  twinPhysicalDeviceMapper.selectSimpleDeviceValue(simpleDeviceValue);
        if (simpleDeviceValue2 == null) {
            return 0.0;
        }
        return simpleDeviceValue2.getValue();
    }
}
